<!-- 收银台 -->
<template>
	<view class="pay-method-wrap">
		<view class="u-flex-col u-col-center money-box" v-if="orderDetail.total_fee">
			<text class="time" v-show="isCountDown">{{ timeText }}</text>
			<view class="money">{{ orderDetail.total_fee }}</view>
		</view>

		<!-- 支付方式单选项 -->
		<u-radio-group v-if="paymentList && paymentList.length&&!flag_big" class="pay-box" v-model="payType"
			active-color="#f0c785">
			<!-- 微信支付 -->
			<view class="u-flex u-row-between pay-item" v-show="paymentList.includes('wechat')"
				@tap="payType = 'wechat'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_wx_pay.png'" mode=""></image>
					<text>微信支付</text>
				</view>
				<u-radio shape="circle" name="wechat"></u-radio>
			</view>
			<!-- 支付宝支付 -->
			<view class="u-flex u-row-between pay-item" v-show="paymentList.includes('alipay')"
				@tap="payType = 'alipay'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_ali_pay.png'" mode=""></image>
					<text>支付宝支付</text>
				</view>
				<u-radio shape="circle" name="alipay"></u-radio>
			</view>
			<!-- 苹果支付 -->
		<!-- 	<view class="u-flex u-row-between pay-item" v-show="paymentList.includes('iospay') && appPlatfrom === 'ios'"
				@tap="payType = 'iospay'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_apple_pay.png'" mode=""></image>
					<text>ApplePay</text>
				</view>
				<u-radio shape="circle" name="iospay"></u-radio>
			</view> -->
			<!-- 余额支付 -->
			<view class="u-flex u-row-between pay-item" v-show="paymentList.includes('wallet')"
				@tap="payType = 'wallet'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_wallet_pay.png'" mode=""></image>
					<text>余额支付</text>
				</view>
				<u-radio shape="circle" name="wallet"></u-radio>
			</view>
			<view class="u-flex u-row-between pay-item" v-show="paymentList.includes('HOME')"
				@tap="payType = 'HOME'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL+'/static/images/BIG-icon/HOMEpay.png'" mode=""></image>
					<text>找家属帮忙付</text>
				</view>
				<u-radio shape="circle" name="HOME"></u-radio>
			</view>
		</u-radio-group>
		
		<view v-else class="box-BIG-paycheck">
			<radio-group style="width: 100%;" @change="radioChange">
				<label @click="checkItem(index)" class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value">
					<view class="namebox">
						<image :src="item.src" mode=""></image> {{item.name}} <text v-if="item.value=='YU_E'" class="yue">当前余额:￥1000</text>
					</view>
					<view>
						<image style="width: 21px;height: 21px;" :src="index===current?$IMG_URL+'/static/images/BIG-icon/rightFill.png':$IMG_URL+'/static/images/BIG-icon/right.png'" mode=""></image>
					</view>
				</label>
			</radio-group>
		</view>

		<button v-if="!flag_big" class="u-reset-button pay-btn" @tap="confirmPay">确认支付 ￥{{ orderDetail.total_fee || '0.00' }}</button>
		<view @click="confirmPay" class="btn-BIG" v-else>
			确认支付
		</view>
		
		<u-popup v-model="show" :closeable="true" mode="bottom" round="15"  @close="close">
			<view class="BIGHomeBox">
				<text class="BIG-title">邀请家属帮忙付款</text>
				    <view class="BIG-info-price">
				        <text class="BIG-title-price">代付金额</text>
				        <text class="BIG-price">￥{{orderDetail.total_fee}}</text>
				    </view>
					<radio-group style="width:82%;background-color: #fffbf1;
    border-radius: 10px;padding: 21px 28px;" @change="radioChange">
						<label @click="checkUserItem(index)" class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in hasList" :key="index">
							<view style="display: flex;align-items: center;">
								<image style="width: 21px;height: 21px;" :src="index===currentUser?$IMG_URL+'/static/images/BIG-icon/rightFill.png':$IMG_URL+'/static/images/BIG-icon/right.png'" mode=""></image>
							</view>
							<view class="namebox">
								<!-- <image style="border-radius: 50%;margin-left: 15px;" :src="item.user.avatar" mode=""></image> -->
								<text :style="{fontSize:flag_big?'18px':'',marginLeft:'15px',maxWidth:'32vw',overflow:'hidden',textOverflow:'ellipsis',whiteSpace:'nowrap'}">{{item.name}}</text>
								<text :style="{fontSize:flag_big?'18px':'',marginLeft:'15px'}">{{item.user.mobile}}</text>
							</view>
							
						</label>
					</radio-group>
					<view @click="sendHome" class="BIG-homepay-btn">
						发给家属
					</view>
			</view>
			
			</u-popup>
	</view>
</template>

<script>
	/**
	 * 接收商品订单orderType:goods，充值订单orderType:recharge
	 */
	import http from '@/shopro/request/index'
	import Pay from '@/shopro/pay';
	import { mapMutations, mapActions, mapState, mapGetters } from 'vuex';
	let timer;
	export default {
		components: {},
		data() {
			return {
				payType: '', //支付方式
				isCountDown: true, //是否显示订单倒计时。
				orderDetail: {},
				timeText: '', //倒计时文本
				platform: this.$platform.get(),
				appPlatfrom: '',
				orderType: '',
				show: false,
				currentUser:null,
				hasList:[],
				// payType:'ZFB',
				items:[
					{
						value: 'alipay',
						name: '支付宝',
						src:this.$IMG_URL+ '/static/images/BIG-icon/ZFB.png'
					},
					{
						value: 'wechat',
						name: '微信',
						src: this.$IMG_URL+'/static/images/BIG-icon/WX.png'
					},
					{
						value: 'HOME',
						name: '找家属帮忙付',
						src: this.$IMG_URL+'/static/images/BIG-icon/HOMEpay.png'
					},
					{
						value: 'wallet',
						name: '余额',
						src:this.$IMG_URL+ '/static/images/BIG-icon/YUEpay.png'
					}
				],
				current:null
			};
		},
		computed: {
			...mapGetters(['initPayment', 'initRecharge']),
			paymentList() {
				let list = []
				switch (this.orderType) {
					case 'goods':
						list = this.initPayment
						break;
					case 'recharge':
						list = this.initPayment?.length && this.initPayment.filter(item => {
							if (this.initRecharge.methods.includes(item)) return item
						})
						break;
					default:
						list = this.initPayment
						break;
				}
				this.payType = list?. [0];
				return list
			},
			// 是否为大字版
			flag_big(){
				return this.$store.state.user.isBIG
			}
		},
		onShow() {
			this.orderDetail.id && this.countDown();
		},
		onLoad() {
			
			this.orderType = this.$Route.query.orderType
			if (this.$platform.get() === 'wxOfficialAccount' && this.$platform.device() === 'ios' && this.$platform
				.entry() !== location.href) location.reload();
				let payArrey = []
				if(this.$Route.query.orderType=='recharge'){
					this.items.filter(rng=>{
						if(rng.value!=='HOME'&&rng.value!=='wallet'){
							payArrey.push(rng)
						}
					})
					this.items = payArrey
				}
				
			// 获取订单详情
			this.getOrderDetail()
			http('user.homeList').then(res=>{
				console.log(res);
				// this.waitList = res.data.auditList
				this.hasList = res.data.userFamilyList
				this.hasList = this.hasList.filter(it=>{
					if(it.status==1){
						return it
					}
				})
				console.log(this.hasList);
				// this.waitList = res.data.filter(it=>{
				// 	if(it.status==0){
				// 		return it
				// 	}
				// })
				// this.hasList = res.data.filter(it=>{
				// 	if(it.status==1){
				// 		return it
				// 	}
				// })
				// console.log(this.waitList,this.hasList);
			})
		},
		onHide() {
			clearInterval(timer);
			timer = null;
		},
		methods: {
			sendHome(){
				console.log(this.hasList[this.currentUser].f_user_id);
				
				http('order.homeOrder',{family_id:this.hasList[this.currentUser].f_user_id,payment:'help_pay',order_sn:this.orderDetail.order_sn}).then(res=>{
					uni.showToast({
						title:'已发送订单至亲属',
						icon:'none'
					})
					setTimeout(function() {
						uni.navigateTo({
							url:'/pages/order/list?type=all'
						})
					}, 1500);
					
				})
			},
			checkUserItem(num){
				this.currentUser = num
			},
			close() {
			          this.show = false
			          // console.log('close');
			        },
			checkItem(i){
				this.current = i
				this.payType = this.items[i].value
			},
			// let payType = ref('ZFB')
			
			radioChange(evt) {
				console.log(evt);
				this.payType = evt.detail.value
			
			},
			// 获取订单详情
			getOrderDetail() {
				switch (this.$Route.query.orderType) {
					case 'goods':
						this.getGoodsOrderDetail()
						break;
					case 'recharge':
						this.getRechargeOrderDetail()
						break;
					default:
						this.getGoodsOrderDetail()
						break;
				}
			},
			// 倒计时
			countDown() {
				let that = this;
				let t = parseInt(that.orderDetail.ext_arr.expired_time * 1000) - parseInt(new Date().getTime());
				t = t / 1000;
				timer = setInterval(() => {
					if (t > 0) {
						let time = that.$tools.format(t);
						that.timeText = `支付剩余时间 ${time.m}:${time.s}`;
						t--;
					} else {
						clearInterval(timer);
						that.timeText = '订单已过期!';
					}
				}, 1000);
			},

			// 发起支付
			confirmPay() {
				let that = this;
				let pay = null;
				if (!that.payType) {
					this.$u.toast('请选择支付方式');
					return;
				}
				if (that.payType === 'wallet') {
					uni.showModal({
						title: '支付提示',
						confirmColor: '#f0c785',
						content: `是否确认使用余额支付:${that.orderDetail.total_fee || '0.00'}元?`,
						success: res => {
							if (res.confirm) {
								pay = new Pay(that.payType, that.orderDetail, that.$Route.query.orderType);
							}
						}
					});
				} else if (that.payType === 'HOME'){
					// 家属支付
					console.log('亲属支付');
					this.show = true
					that.show = true
				} else {
					pay = new Pay(that.payType, that.orderDetail, that.$Route.query.orderType);
				}
			},

			// 支付商品订单信息
			getGoodsOrderDetail() {
				let that = this;
				that.$http('order.detail', {
					id: that.$Route.query.orderId
				}).then(res => {
					if (res.code === 1) {
						that.orderDetail = res.data;
						if (res.data.ext_arr !== null) {
							that.countDown();
						} else {
							that.isCountDown = false;
						}
					}
				});
			},

			// 充值订单信息
			getRechargeOrderDetail() {
				let that = this;
				that.$http('money.rechargeDetail', {
					id: that.$Route.query.orderId
				}).then(res => {
					if (res.code === 1) {
						that.orderDetail = res.data;
						if (res.data.ext_arr !== null) {
							that.countDown();
						} else {
							that.isCountDown = false;
						}
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	.BIGHomeBox {
		text-align: center;
		padding: 21px 10px 20px;
		box-sizing: border-box;
		color: #000;
		.BIG-title {
			font-size: 20px;
				font-weight: bold;
		}
		.BIG-info-price {
			text {
				display: block;
			}
			.BIG-title-price {
				font-size: 15px;
				color: #aaa;
				margin-top: 30px;
				margin-bottom: 18px;
			}
			.BIG-price {
				font-size: 28px;
					font-weight: bold;
					color: #ff2400;
					margin-bottom: 50px;
			}
		}
		.BIG-homepay-btn {
			line-height: 45px;
			font-size: 20px;
				font-weight: bold;
				margin-top: 25px;
			color: #fff;
			height: 45px;
				background-image: linear-gradient(90deg, 
					#ff8300 0%, 
					#ff3d00 100%), 
				linear-gradient(
					#ffffff, 
					#ffffff);
				background-blend-mode: normal, 
					normal;
				border-radius: 23px;
		}
	}
	.box-BIG-paycheck {
		background-color: #fff;
		width: 94%;
		margin: 0 auto;
		border-radius: 10px;
		box-sizing: border-box;
		padding: 23px 22px 22px 18px;
	}
	
	.uni-list-cell {
		width: 100%;
		display: flex;
		justify-content: space-between;
		font-size: 16px;
		color: #333;
		align-items: center;
		vertical-align: top;
		margin-bottom: 26px;
		&:last-child {
			margin-bottom: 0;
		}
		.yue {
			font-size: 13px;
			color: #999;
			margin-left: 10px;
		}
		.namebox {
			display: flex;
			align-items: center;
			flex: 1;
			image {
				margin-right: 13px;
				width: 21px;
				height: 21px;
			}
		}
	}
	
	.btn-BIG {
		height: 40px;
		width: 85%;
		margin: 0 auto;
		margin-top: 25px;
			background-image: linear-gradient(90deg, 
				#ff8300 0%, 
				#ff3d00 100%), 
			linear-gradient(
				#ffffff, 
				#ffffff);
			background-blend-mode: normal, 
				normal;
			border-radius: 20px;
			text-align: center;
			line-height: 40px;
			font-size: 20px;
				font-weight: bold;
				color: #fff;
	}
	.money-box {
		background: #fff;
		height: 250rpx;
		margin-bottom: 20rpx;
		padding-top: 30rpx;

		.time {
			font-size: 28rpx;
			color: #c4c4c4;
		}

		.money {
			color: #e1212b;
			font-size: 60rpx;
			margin-top: 60rpx;

			&::before {
				content: '￥';
				font-size: 46rpx;
			}
		}
	}

	.pay-box {
		.pay-item {
			height: 90rpx;
			padding: 0 30rpx;
			font-size: 26rpx;
			background: #fff;
			width: 750rpx;
			border-bottom: 1rpx solid #eeeeee;

			&:last-child {
				border-bottom: none;
			}

			.pay-img {
				width: 40rpx;
				height: 40rpx;
				margin-right: 25rpx;
			}
		}
	}

	.pay-btn {
		width: 690rpx;
		line-height: 80rpx;
		background: linear-gradient(90deg, rgba(240, 199, 133, 1), rgba(246, 214, 157, 1));
		border-radius: 40rpx;
		color: rgba(#fff, 0.9);
		margin: 100rpx auto 0;
	}
</style>
